package org.qpcr.community.project.web;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.hswebframework.ezorm.rdb.mapping.ReactiveQuery;
import org.hswebframework.ezorm.rdb.mapping.defaults.SaveResult;
import org.hswebframework.web.api.crud.entity.QueryParamEntity;
import org.hswebframework.web.authorization.Authentication;
import org.hswebframework.web.authorization.annotation.Authorize;
import org.hswebframework.web.authorization.annotation.Resource;
import org.hswebframework.web.authorization.annotation.SaveAction;
import org.hswebframework.web.crud.web.reactive.ReactiveServiceCrudController;
import org.qpcr.community.device.entity.DeviceInstanceEntity;
import org.qpcr.community.device.service.LocalDeviceInstanceService;
import org.qpcr.community.project.model.entity.DeviceVersionEntity;
import org.qpcr.community.project.service.DeviceVersionService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 软件版本列表管理
 */
@RestController
@RequestMapping( {"/device/version"})
@Authorize
@Resource(id = "device-version", name = "软件版本更新/同步管理")
@Slf4j
@Tag(name = "软件版本列表管理")
@RequiredArgsConstructor
public class DeviceVersionController implements ReactiveServiceCrudController<DeviceVersionEntity, String> {


    @Getter
    private final DeviceVersionService service;
    private final LocalDeviceInstanceService deviceInstanceService;


    @PostMapping("/no-auth")
    @SaveAction
    @Operation(
        summary = "新增单个数据,并返回新增后的数据."
    )
    @Authorize(ignore = true)
    public Mono<SaveResult> addOverride(@RequestBody Flux<DeviceVersionEntity> payload) {
        return ((ReactiveQuery)deviceInstanceService.createQuery().setParam(new QueryParamEntity()))
                .fetch()
                .collectList()
                .flatMap(list -> {
                    List<DeviceInstanceEntity> deviceInstanceEntities = (List<DeviceInstanceEntity>) list;
                    return Authentication
                            .currentReactive()
                            .flatMapMany(auth -> payload.map(entity -> applyAuthentication(entity, auth)))
                            .switchIfEmpty(payload)
                            .collectList()
                            .flatMap(deviceVersionEntityList -> {
                                deviceVersionEntityList.forEach(deviceVersionEntity -> {
                                    // 过滤只剩下相同设备id的信息
                                    List<DeviceInstanceEntity> fil = deviceInstanceEntities.stream()
                                            .filter(deviceInstanceEntity -> deviceInstanceEntity.getId().equals(deviceVersionEntity.getDeviceId()))
                                            .collect(Collectors.toList());
                                    if (fil.size() > 0) {
                                        deviceVersionEntity.setDeviceName(fil.get(0).getName());
                                    }
                                });
                                return getService().save(deviceVersionEntityList);
                            });
                });


//        return Authentication
//            .currentReactive()
//            .flatMapMany(auth -> payload.map(entity -> applyAuthentication(entity, auth)))
//            .switchIfEmpty(payload)
//            .collectList();
//        return payload.flatMap(getService()::save).collectList();
//        payload.collectList().flatMap(f --> this.save(f));
//        return this.add(payload);
    }

}
